package com.bwie.parkdemo.mvvm.viewmodel;

import android.app.Application;
import android.databinding.ObservableField;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.View;

import com.bwie.lib_core.action.ActActionField;
import com.bwie.lib_core.base.BaseEntity;
import com.bwie.lib_core.mvvm.base.BaseOBViewModel;
import com.bwie.parkdemo.mvvm.model.PatrolesModel;
import com.bwie.parkdemo.mvvm.model.entity.PatrolesEntity;
import com.bwie.parkdemo.mvvm.ui.activity.PatrolesActivity;
import com.bwie.parkdemo.mvvm.ui.activity.PatrolesValuesActivity;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * 全部巡更路线vm
 * */
public class PatrolesViewModel extends BaseOBViewModel<PatrolesModel>{

    public ObservableField<List<Object>>datas;
    public PatrollesOnItemClickListener itemListener;
    public int adapterType = 8;

    public PatrolesViewModel(@NonNull Application application) {
        super(application);
        datas = new ObservableField<>();
        itemListener = new PatrollesOnItemClickListener();
    }

    @Override
    protected PatrolesModel createModel() {
        return new PatrolesModel();
    }

    @Override
    protected void initData() {
        m.request(null).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(this);
    }

    @Override
    public void onNext(BaseEntity baseEntity) {
        if (!baseEntity.statuesCode.equals("200")){
            showMsg(baseEntity.msg);
            return;
        }
        PatrolesEntity entity = (PatrolesEntity) baseEntity;
        datas.set(entity.getDatas());
    }

    //RecyclerviewItem点击监听
    public class PatrollesOnItemClickListener implements BaseQuickAdapter.OnItemClickListener{

        @Override
        public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
            PatrolesEntity.Values values = (PatrolesEntity.Values) datas.get().get(position);
            Bundle bundle = new Bundle();
            bundle.putSerializable("Values",values);
            Map<String,Object>map = new HashMap<>();
            map.put(ActActionField.ACTCLAZZ, PatrolesValuesActivity.class);
            map.put(ActActionField.ACTBUNDLE,bundle);
            startAct(map);
        }
    }

}
